package com.lang.annotation;

import java.lang.annotation.Annotation;

@MarkerAnnotationCase
@RepeatableFilter("Hello")
@RepeatableFilter("World")
@ClassAnnotation(author = "zj", date = "2018/3/24")
public class MainAnnotation {

    @MarkerAnnotationCase
    public void foo() {
    }

    public static void main(String[] args) {
//        isAnnotationPresent();
//        getAnnotations();
        getAnnotationsByType();
    }

    public static void isAnnotationPresent() {
        MainAnnotation mainAnnotation = new MainAnnotation();
        // object class
        Class<MainAnnotation> objectClass = MainAnnotation.class;
        // the runtime class of this object
        Class<? extends MainAnnotation> runtimeClass = mainAnnotation.getClass();
        // 方法 isAnnotationPresent 仅能验证是否存在标注在元素上的注解
        System.out.println(objectClass.isAnnotationPresent(ClassAnnotation.class));
        System.out.println(runtimeClass.isAnnotationPresent(ClassAnnotation.class));
        System.out.println(objectClass.isAnnotationPresent(MarkerAnnotationCase.class));
        // 使用 object class 输出注解内容
        ClassAnnotation classAnnotation = objectClass.getAnnotation(ClassAnnotation.class);
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(classAnnotation.author()).append(" ").append(classAnnotation.date());
        // 使用 the runtime class of this object 输出注解内容
        ClassAnnotation classAnnotation1 = runtimeClass.getAnnotation(ClassAnnotation.class);
        stringBuilder.append("\n").append(classAnnotation1.author()).append(" ").append(classAnnotation1.date());
        System.out.println(stringBuilder.toString());
    }

    public static void getAnnotations() {
        Class<MainAnnotation> objectClass = MainAnnotation.class;
        Annotation[] annotations = objectClass.getAnnotations();
        for (Annotation annotation : annotations) {
            System.out.println(annotation);
        }
    }

    public static void getAnnotationsByType() {
        Class<MainAnnotation> objectClass = MainAnnotation.class;
        MarkerAnnotationCase[] markerAnnotationCases = objectClass.getAnnotationsByType(MarkerAnnotationCase.class);
        for (MarkerAnnotationCase markerAnnotationCase : markerAnnotationCases)
            System.out.println(markerAnnotationCase);
        RepeatableFilter[] repeatableFilters = objectClass.getAnnotationsByType(RepeatableFilter.class);
        for (RepeatableFilter repeatableFilter : repeatableFilters)
            System.out.println(repeatableFilter);
        ClassAnnotation[] classAnnotations = objectClass.getAnnotationsByType(ClassAnnotation.class);
        for (ClassAnnotation classAnnotation : classAnnotations)
            System.out.println(classAnnotation);
//        RepeatableFilters[] repeatableFilters1 = objectClass.getAnnotationsByType(RepeatableFilter.class);
//        for (RepeatableFilters repeatableFilter : repeatableFilters1)
//            System.out.println(repeatableFilter);
//        RuntimeVisibleAnnotation
    }
}

